/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui.logic;

import com.itextpdf.text.Document;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit.CopyAction;
import javax.swing.text.DefaultEditorKit.CutAction;
import javax.swing.text.DefaultEditorKit.PasteAction;
import javax.swing.undo.UndoManager;
import project.jpdfexport.constants.JPDFConst.ALIGNMENT;
import project.jpdfexport.constants.JPDFConst.STYLE;
import project.jpdfexport.jpdf.PDFText;
import project.jpdftools.other.IntegerTextField;
import project.jpdftools.ui.actionlisteners.*;
import project.jpdftools.ui.keylisteners.ShortcutsAction;
import project.jpdftools.managers.LocalizationManager;

/**
 *
 * @author Mido
 */
public final class JPDFText extends JPDF {

    private int spaceAfter;
    private Color fillColor;
    private boolean showBorder;
    private int fontSize;
    private int textAlignment;
    private String fontPathName;
    private Color fontColor;
    private int fontStyle;
    private Color borderColor;
    // UI components
    private JTextPane textPane = new JTextPane();
    private JButton btSpaceAfterOk = new JButton(LocalizationManager.getCurrent_resource().getString("OK"));
    private IntegerTextField itfSpaceAfter = new IntegerTextField();
    // menu items
    private JMenuItem miFontProperties = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_FONT_PROPERTIES"),
            new ImageIcon("Data/Icons/Menu Items/font-true-type-icon.png"));
    private JMenuItem miFillColor = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_FILL_COLOR"),
            new ImageIcon("Data/Icons/Menu Items/fill_color.gif"));
    private JMenuItem miFontColor = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_FONT_COLOR"),
            new ImageIcon("Data/Icons/Menu Items/text_color.gif"));
    private JMenuItem mBorderColor = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_BORDER_COLOR"),
            new ImageIcon("Data/Icons/Menu Items/border_color.png"));
    private JMenuItem miShowBorder = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_HIDE_BORDER"),
            new ImageIcon("Data/Icons/Menu Items/show_border.png"));
    private JMenu mSpaceAfter = new JMenu(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_SPACE_AFTER_TEXT"));
    private JMenuItem miCut = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_CUT"),
            new ImageIcon("Data/Icons/Menu Items/Cut.png"));
    private JMenuItem miCopy = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_COPY"),
            new ImageIcon("Data/Icons/Menu Items/Copy.png"));
    private JMenuItem miPaste = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_PASTE"),
            new ImageIcon("Data/Icons/Menu Items/Paste.png"));
    private JMenuItem miUndo = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_UNDO"),
            new ImageIcon("Data/Icons/Menu Items/Undo.png"));
    private JMenuItem miRedo = new JMenuItem(LocalizationManager.getCurrent_resource().getString("JPDFTEXT_REDO"),
            new ImageIcon("Data/Icons/Menu Items/Redo.png"));
    private UndoManager undoManager = new UndoManager();

    public JPDFText() {
        // default values
        this.spaceAfter = 5;
        this.fillColor = Color.white;
        this.showBorder = true;
        this.fontSize = 12;
        this.fontColor = Color.black;
        this.fontStyle = STYLE.normal;
        this.scrollPane = new JScrollPane(textPane);
        this.textAlignment = ALIGNMENT.left;
        this.borderColor = Color.black;
    }

    @Override
    public void createUI() {
        // get the screen resolution
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        scrollPane.setPreferredSize(new Dimension(screen.width - 180, 100));
        scrollPane.setViewportView(textPane);
        panel.setPreferredSize(new Dimension(screen.width - 180, 130));

        // add components to the panel
        panel.add(scrollPane);

        // add menu items
        addMenuItems();
        // assign shortcuts to menu items
        addMenuItemsShortcuts();
    }

    @Override
    public void export(Document document) {
        if (fontPathName != null) {
            PDFText.addText(document, textPane.getText(), project.jpdftools.toolkit.Toolkit.getFontPath(fontPathName), spaceAfter, fillColor, showBorder, fontSize,
                    fontColor, fontStyle, textAlignment, borderColor);
        } else {
            PDFText.addText(document, textPane.getText(), null, spaceAfter, fillColor, showBorder, fontSize,
                    fontColor, fontStyle, textAlignment, borderColor);
        }
    }

    /**
     * Add menu items to the pop up menu
     */
    private void addMenuItems() {
        // add the undo manager to the text pane document
        textPane.getDocument().addUndoableEditListener(undoManager);

        itfSpaceAfter.setColumns(10);
        itfSpaceAfter.setText(String.valueOf(spaceAfter));
        // border is intially visible
        miShowBorder.setSelected(true);

        // default values
        miAlignLeft.setSelected(true);

        // add menu items to the popup menu
        mTextAlignment.add(miAlignLeft);
        mTextAlignment.add(miAlignCenter);
        mTextAlignment.add(miAlignRight);
        mSpaceAfter.add(itfSpaceAfter);
        mSpaceAfter.add(btSpaceAfterOk);
        menu.add(miCut);
        menu.add(miCopy);
        menu.add(miPaste);
        menu.add(new JSeparator());
        menu.add(miUndo);
        menu.add(miRedo);
        menu.add(new JSeparator());
        menu.add(mBorderColor);
        menu.add(miFillColor);
        menu.add(miFontColor);
        menu.add(new JSeparator());
        menu.add(miFontProperties);
        menu.add(miRemoveComponent);
        menu.add(miShowBorder);
        menu.add(mSpaceAfter);
        menu.add(mTextAlignment);

        // when in the focus of the text field, you can press enter on the keyboard
        // instead of clicking on the OK button
        itfSpaceAfter.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // check if the user pressed delete key
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    btSpaceAfterOk.doClick();
                }
            }
        });

        // create listeners
        textPane.addMouseListener(new ShowMenuAction(this));
        // font properties
        miFontProperties.addActionListener(new FontPropertiesAction(this));
        // font color
        miFontColor.addActionListener(new FontColorAction(this));
        // component fill color
        miFillColor.addActionListener(new FillColorAction(this));
        // show/hide border
        miShowBorder.addActionListener(new ShowBorderAction(this));
        // set space after
        btSpaceAfterOk.addActionListener(new SpaceAfterAction(this));
        // center text alignment
        miAlignCenter.addActionListener(new AlignCenterAction(this));
        // left text alignment
        miAlignLeft.addActionListener(new AlignLeftAction(this));
        // right text alignment
        miAlignRight.addActionListener(new AlignRightAction(this));
        // border color
        mBorderColor.addActionListener(new BorderColorAction(this));
        // cut action
        miCut.addActionListener(new CutAction());
        // copy action
        miCopy.addActionListener(new CopyAction());
        // paste action
        miPaste.addActionListener(new PasteAction());
        // undo action
        miUndo.addActionListener(new UndoAction(this, undoManager));
        // redo action
        miRedo.addActionListener(new RedoAction(this, undoManager));
        // text pane key action listener + shortcuts
        textPane.addKeyListener(new ShortcutsAction(this));
    }

    /**
     * Add shortcuts for the menu items
     */
    private void addMenuItemsShortcuts() {
        miCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
        miCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
        miPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
        miUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
        miRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
        miRemoveComponent.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, ActionEvent.SHIFT_MASK));
        miAlignCenter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
        miAlignLeft.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
        miAlignRight.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
        mBorderColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
        miFillColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
        miFontColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
        miFontProperties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
        miShowBorder.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color border_color) {
        this.borderColor = border_color;
    }

    public void setFillColor(Color fill_color) {
        this.fillColor = fill_color;
    }

    public void setFontColor(Color font_color) {
        this.fontColor = font_color;
    }

    public String getFontPathName() {
        return fontPathName;
    }

    public void setFontPathName(String font_path_name) {
        this.fontPathName = font_path_name;
    }

    public void setFontSize(int font_size) {
        this.fontSize = font_size;
    }

    public void setFontStyle(int font_style) {
        this.fontStyle = font_style;
    }

    public IntegerTextField getItfSpaceAfter() {
        return itfSpaceAfter;
    }

    public JMenuItem getMiShowBorder() {
        return miShowBorder;
    }

    public void setShowBorder(boolean show_border) {
        this.showBorder = show_border;
    }

    public void setSpaceAfter(int space_after) {
        this.spaceAfter = space_after;
    }

    public JTextPane getTextPane() {
        return textPane;
    }

    public void setTextAlignment(int text_alignment) {
        this.textAlignment = text_alignment;
    }

    public int getFontSize() {
        return fontSize;
    }

    public int getFontStyle() {
        return fontStyle;
    }

    public boolean isShowBorder() {
        return showBorder;
    }

    public JMenuItem getMiRedo() {
        return miRedo;
    }

    public JMenuItem getMiUndo() {
        return miUndo;
    }

    public JMenuItem getMiBorderColor() {
        return mBorderColor;
    }

    public JMenuItem getMiFillColor() {
        return miFillColor;
    }

    public JMenuItem getMiFontColor() {
        return miFontColor;
    }

    public JMenuItem getMiFontProperties() {
        return miFontProperties;
    }
}
